var deviceone = require("deviceone");
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var alipay = sm("do_Alipay");
var wxpay = sm("do_TencentWX");
var http = require("helper/http");
var payUrl = require("url/pay");
var openHelper = require("helper/open");

page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "收银台";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePageToID({}, "", "pay_product_back", function(data, e) {
    });
    app.closePage();
});

var preData = page.getData();
var mins = parseInt(preData.leftSeconds/60);
var seconds = preData.leftSeconds%60;
ui("leftSeconds").text=mins+"分"+seconds+"秒";
ui("orderTitle").text=preData.title;
var paredStr = "";
if(preData.payed&&preData.payed!=="0.0"){
	paredStr = "(已支付￥"+preData.payed+")";
}
ui("orderNeedPay").text="￥"+preData.total+paredStr;
ui("do_Button_1").text = "在线支付￥"+preData.type4Online;

var gridData = mm("do_ListData");
var listViewCurrency = ui("listViewCurrency");

var mTimer = mm("do_Timer", "timer4pay", "app");
mTimer.delay = 0;
mTimer.interval = 1000;
if (!mTimer.isStart()) {
    mTimer.start();
}
mTimer.on("tick", function(data,e){
	ui("leftSeconds").text=mins+"分"+seconds+"秒";
    if (mins==0&&seconds==0) {
            mTimer.stop();
    }else{
    	seconds = seconds - 1;
    	if(seconds<=0){
    		if(mins>0){
    			mins = mins - 1;
    			seconds=59;
    		}
    	}
    }
});

//支付方式：1-圈币;2-支付宝;3-微信;4-余额
var payWay = 2;
//抵扣余额值
var deduction = preData.deduction;
//支付页面是否展示(启用)余额抵扣选项
var displayDeduction = (preData.payWay.qquan==="1");
var deductionAll = (preData.deductionAll==="1");
//是否选择用余额抵扣
var flag = false;

//是否选中圈币支付
var isQuanCheck = false;
//是否选中余额支付
var isMoneyCheck = false;
//是否选中在线支付
var isOnlineCheck = true;
//抵扣圈币值
var ddtQuan = 0;
//抵扣余额值
var ddtMoney = 0;
//在线支付值
var ddtOnline = preData.type4Online;

var listViewPay = ui("listViewPay");
var listdata = mm("do_ListData");
listViewPay.bindItems(listdata);
listdata.addData([{
	"isVisible" : preData.payWay.ali==="1",
	"payLogo" : "source://image/common/zhifubao.png",
    "payName" : "支付宝",
    "labelRadio" : "source://image/common/radiocheck.png"
},
{
	"isVisible" : preData.payWay.weixin==="1",
    "payLogo" : "source://image/common/weixin.png",
    "payName" : "微信",
    "labelRadio" : "source://image/common/radio.png"
}]);
listViewPay.refreshItems();
listViewPay.on("touch", function(index) {
	//如果此前选择了完全抵扣，当点击支付宝或微信时，要把抵扣的选项去掉
	if((isQuanCheck==true||isMoneyCheck==true) && ddtOnline==0 && isOnlineCheck==false){
		for (var i = 0; i < gridData.getCount(); i++) {
			var d = gridData.getOne(i);
			d.checkboxImg = "source://image/common/checkbox.png";
			gridData.updateOne(i, d);
		}
		isQuanCheck = false;
		isMoneyCheck = false;
		ddtQuan = 0;
		ddtMoney = 0;
		ddtOnline = preData.type4Online;
		listViewCurrency.refreshItems();
		changePayTips();
	}
	for (var i = 0; i < listdata.getCount(); i++) {
		var d = listdata.getOne(i);
		if(!d.isVisible) return;
		if (index == i){
			d.labelRadio = "source://image/common/radiocheck.png";
			if(index==0){
				payWay = 2;
			}else if(index==1){
				payWay = 3;
			}
		}else{
			d.labelRadio = "source://image/common/radio.png";
		}
		listdata.updateOne(i, d);
	}
	isOnlineCheck = true;
	listViewPay.refreshItems();
});

listViewCurrency.bindItems(gridData);
if(preData.payWay.money==="1"){
	gridData.addOne({
		"checkboxImg" : "source://image/common/checkbox.png",
		"currencyText" : "最多可用"+preData.type3Money+"余额",
		"currencyPrice" : "抵￥"+preData.type3MoneyRMB,
		"checkboxImgVisible" : true
	});
	listViewCurrency.refreshItems();
}
if(preData.payWay.qquan==="1"&&preData.useQuanPay==="1"){
	if(preData.hasQuan==="2"){
		gridData.addOne({
			"checkboxImg" : "source://image/common/checkbox.png",
			"currencyText" : "您暂时无可用圈币进行消费抵扣",
			"currencyPrice" : "",
			"checkboxImgVisible" : false
		});
	}else{
		if(preData.hasMayPayWithQuan==="2"){
			gridData.addOne({
				"checkboxImg" : "source://image/common/checkbox.png",
				"currencyText" : "该订单目前暂无额度允许使用圈币消费抵扣",
				"currencyPrice" : "",
				"checkboxImgVisible" : false
			});
		}else{
			gridData.addOne({
				"checkboxImg" : "source://image/common/checkbox.png",
				"currencyText" : "最多可用"+preData.type1Quan+"圈币",
				"currencyPrice" : "抵￥"+preData.type1QuanRMB,
				"checkboxImgVisible" : true
			});
		}
	}
	/*修改高度*/
	//listViewCurrency.height = 140;
	//listViewCurrency.redraw();
	listViewCurrency.refreshItems();
}
//订阅GridView的touch点击事件
listViewCurrency.on("touch", function(data) {
	if(data==1){//点击圈币支付
		if(preData.payWay.qquan==="1"&&preData.hasQuan==="1"&&preData.hasMayPayWithQuan==="1"){
			if(isQuanCheck==false){//如果刚开始是没选中的
				isQuanCheck=true;
				var d1 = gridData.getOne(1);
				d1.checkboxImg = "source://image/common/checkboxcheck.png";
				gridData.updateOne(1, d1);
				listViewCurrency.refreshItems();
				ddtQuan = preData.type1Quan;
				if(preData.isAllQuan==="1"){//圈币可以完全抵扣
					ddtQuan = preData.type1Quan;
					isQuanCheck = true;
					if(isMoneyCheck==true){//去掉余额勾选
						isMoneyCheck==false;
						var d = gridData.getOne(0);
						d.checkboxImg = "source://image/common/checkbox.png";
						gridData.updateOne(0, d);
						listViewCurrency.refreshItems();
					}
					if(isOnlineCheck==true){
						isOnlineCheck==false;
						for (var j = 0; j < listdata.getCount(); j++) {
							var dd = listdata.getOne(j);
							dd.labelRadio = "source://image/common/radio.png";
							listdata.updateOne(j, dd);
						}
						listViewPay.refreshItems();
					}
					ddtMoney = 0;
					ddtOnline = 0;
				}else{
					if(isMoneyCheck==true){
						ddtMoney = preData.type2Money;
						if(preData.isAllQNM==="1"){
							ddtOnline = 0;
							isOnlineCheck=false;
							for (var j = 0; j < listdata.getCount(); j++) {
								var dd = listdata.getOne(j);
								dd.labelRadio = "source://image/common/radio.png";
								listdata.updateOne(j, dd);
							}
							listViewPay.refreshItems();
						}else{
							ddtOnline = preData.type2Online;
						}
					}else{
						ddtOnline = preData.type1Online;
					}
				}
			}else{//取消选择
				isQuanCheck=false;
				ddtQuan=0;
				var d1 = gridData.getOne(1);
				d1.checkboxImg = "source://image/common/checkbox.png";
				gridData.updateOne(1, d1);
				listViewCurrency.refreshItems();
				
				if(isMoneyCheck==true){
					ddtMoney = preData.type3Money;
					ddtOnline = preData.type3Online;
					if(preData.isAllMoney==="1"){
						ddtOnline = 0;
						isOnlineCheck=false;
						for (var j = 0; j < listdata.getCount(); j++) {
							var dd = listdata.getOne(j);
							dd.labelRadio = "source://image/common/radio.png";
							listdata.updateOne(j, dd);
						}
						listViewPay.refreshItems();
					}else{
						ddtOnline = preData.type3Online;
					}
				}else{
					ddtOnline = preData.type4Online;
				}
			}
		}
	}
	if(data==0){//点击余额支付
		if(isMoneyCheck==false){//勾选余额
			isMoneyCheck=true;
			var d0 = gridData.getOne(0);
			d0.checkboxImg = "source://image/common/checkboxcheck.png";
			gridData.updateOne(0, d0);
			listViewCurrency.refreshItems();
			if(preData.isAllMoney==="1"){
				ddtMoney = preData.type3Money;
				ddtQuan = 0;
				ddtOnline = 0;
				if(isQuanCheck==true){
					isQuanCheck=false;
					var d = gridData.getOne(1);
					d.checkboxImg = "source://image/common/checkbox.png";
					gridData.updateOne(1, d);
					listViewCurrency.refreshItems();
				}
				if(isOnlineCheck==true){
					isOnlineCheck=false;
					for (var j = 0; j < listdata.getCount(); j++) {
						var dd = listdata.getOne(j);
						dd.labelRadio = "source://image/common/radio.png";
						listdata.updateOne(j, dd);
					}
					listViewPay.refreshItems();
				}
			}else{
				ddtMoney = preData.type2Money;
				if(isQuanCheck==true){
					ddtQuan = preData.type1Quan;
					ddtOnline = preData.type2Online;
					if(preData.isAllQNM==="1"){
						if(isOnlineCheck==true){
							isOnlineCheck=false;
							for (var j = 0; j < listdata.getCount(); j++) {
								var dd = listdata.getOne(j);
								dd.labelRadio = "source://image/common/radio.png";
								listdata.updateOne(j, dd);
							}
							listViewPay.refreshItems();
						}
					}
				}else{
					ddtQuan = 0;
					ddtOnline = preData.type3Online;
				}
			}
		}else{//取消选择
			var d0 = gridData.getOne(0);
			d0.checkboxImg = "source://image/common/checkbox.png";
			gridData.updateOne(0, d0);
			listViewCurrency.refreshItems();
			isMoneyCheck=false;
			ddtMoney = 0;
			if(isQuanCheck==true){
				ddtOnline = preData.type1Online;
			}else{
				ddtOnline = preData.type4Online;
			}
		}
	}
	changePayTips();
	/*
	for (var i = 0; i < gridData.getCount(); i++) {
		var d = gridData.getOne(i);
		if(flag==true){
			flag = false;
			d.checkboxImg = "source://image/common/checkbox.png";
			changePayTips();
		}else{
			flag = true;
			d.checkboxImg = "source://image/common/checkboxcheck.png";
			//如果可以全部抵扣，则去掉支付宝和微信的在线支付
			if(deductionAll==true){
				payWay = 1;
				for (var j = 0; j < listdata.getCount(); j++) {
					var dd = listdata.getOne(j);
					dd.labelRadio = "source://image/common/radio.png";
					listdata.updateOne(j, dd);
				}
				listViewPay.refreshItems();
			}
			changePayTips();
		}
		gridData.updateOne(i, d);
	}
	listViewCurrency.refreshItems();
	*/
});

function changePayTips(){
	var quanStr = "";
	var moneyStr = "";
	var onlineStr = "";
	var tipss = "";
	if(ddtQuan>0){
		quanStr = ddtQuan+"圈币";
	}
	if(ddtMoney>0){
		moneyStr = ddtMoney+"余额";
	}
	if(ddtOnline>0){
		onlineStr = "在线支付￥"+ddtOnline;
	}
	if(quanStr!==""&&moneyStr!==""){
		tipss = "使用"+quanStr+"和"+moneyStr+"一起抵扣";
	}else if(quanStr!==""){
		tipss = "使用"+quanStr+"抵扣";
	}else if(moneyStr!==""){
		tipss = "使用"+moneyStr+"抵扣";
	}
	if(onlineStr!==""){
		if(tipss!==""){
			tipss = tipss+",还须"+onlineStr;
		}else{
			tipss = onlineStr;
		}
	}
	ui("do_Button_1").text = tipss;
}
var orderNo = preData.orderNo;
ui("do_Button_1").on("touch", function(data, e) {
	if(!orderNo || orderNo.trim()===""){
		nf.toast("系统异常");
		return;
	}
	if(mins==0&&seconds==0){
		nf.toast("已超过了规定的支付时间");
		return;
	}
	if(ddtOnline>0&&isOnlineCheck==false){
		nf.toast("请选择在线支付方式");
		return;
	}
	if(ddtOnline==0 && ((isQuanCheck==true&&preData.isAllQuan==="1")||(isMoneyCheck==true&&preData.isAllMoney==="1")||(isQuanCheck==true&&isMoneyCheck==true&&preData.isAllQNM==="1"))){//全部抵扣支付
		http.post(payUrl.pay.payDeduction,{"orderNo":orderNo,"ddtQuan":""+ddtQuan,"ddtMoney":""+ddtMoney,"ddtOnline":""+ddtOnline},function(data){
			nf.toast("抵扣支付成功");
    		openHelper.open("source://view/order/pay/success.ui",{"orderNo":orderNo});
		});
	}else{
		http.post(payUrl.pay.getPayByOrderNo,{"orderNo":orderNo,"payWay":""+payWay,"ddtQuan":""+ddtQuan,"ddtMoney":""+ddtMoney,"ddtOnline":""+ddtOnline},function(data){
			if(payWay==2){
				alipay.pay({
				    rsaPrivate:data.aliPay.rsaPrivate,
				    rsaPublic:data.aliPay.rsaPublic,
				    partner:data.aliPay.partner,
				    notifyUrl:data.aliPay.notifyUrl,
				    tradeNo:data.aliPay.tradeNo,
				    subject:data.aliPay.subject,
				    sellerId:data.aliPay.sellerId,
				    totalFee:data.aliPay.totalFee,
				    body:data.aliPay.body,
				    timeOut:data.aliPay.timeOut}, 
				    function(aliData, e){
				    	if(aliData&&aliData.code){
				    		if(aliData.code==="9000"){
				    			nf.toast("支付成功");
					    		openHelper.open("source://view/order/pay/success.ui",{"orderNo":orderNo});
				    		}else{
				    			if(aliData.code==="8000"){
				    				nf.toast("正在处理中");
					    		}else if(aliData.code==="6002"){
					    			nf.toast("网络连接出错");
					    		}else if(aliData.code==="6001"){
					    			nf.toast("已取消支付");
					    		}else if(aliData.code==="4000"){
					    			nf.toast("支付失败");
					    		}else{
									nf.toast("即将跳到订单详情");
				    			}
				    			openHelper.open("source://view/order/detail.ui",{"orderNo":orderNo,"fromPage":"pay_main"});
				    		}
				    	}else{
				    		nf.toast("未完成支付");
				    		openHelper.open("source://view/order/detail.ui",{"orderNo":orderNo,"fromPage":"pay_main"});
				    	}
				    }
				);
			}else if(payWay==3){
				var isInstalled = wxpay.isWXAppInstalled();
				if(isInstalled && isInstalled==true){
					wxpay.pay({
						appId:data.wxPay.appId,
						partnerId:data.wxPay.partnerId,
						prepayId:data.wxPay.prepayId,
						package:data.wxPay.packageStr,
						nonceStr:data.wxPay.nonceStr,
						timeStamp:data.wxPay.timeStamp,
						sign:data.wxPay.sign},
						function(wxData, e){
							if(wxData==0){
								nf.toast("支付成功");
								openHelper.open("source://view/order/pay/success.ui",{"orderNo":orderNo});
							}else{
								 if(wxData==-1){
									 nf.toast("支付异常，请重新再试");
								 }else if(wxData==-2){
									 nf.toast("已取消支付");
								 }else{
									 nf.toast("未完成支付");
								 }
								 openHelper.open("source://view/order/detail.ui",{"orderNo":orderNo,"fromPage":"pay_main"});
							}
						}
					);
				}else{
					nf.toast("需要安装微信APP");
				}
			}else{
				nf.toast("请选择支付方式");
				return;
			}
		});
	}
});